﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp1
{
    internal class Obstacle
    {
        public int X;
        public int Y;
        public int Width;
        public int Height;
        public int Life;
        public bool IsDead = false;
        public static int CanvasHeight = 480; // 画布高度，确保与游戏窗口一致
        private Image obstacleImage; 

        private static Random rand = new Random();

        public Obstacle(int x, int y, int width, int height)
        {
            X = x;
            Y = y;
            Width = width;
            Height = height;
            Life = rand.Next(50, 100); // 随机生成生命值 5~20
            obstacleImage = Image.FromFile("gif\\obstacle.png");
        }

        public void Move()
        {
            Y += 1; // 障碍物向下移动
        }

        public void Draw(Graphics g)
        {
            g.DrawImage(obstacleImage, X, Y, Width, Height); // 绘制障碍物

            // 绘制生命条背景
            g.FillRectangle(Brushes.Gray, X, Y - 10, Width, 5);

            // 绘制生命条（绿色部分根据剩余生命值比例绘制）
            float lifePercentage = (float)Life / 100; // 最大生命值为 20
            int lifeBarWidth = (int)(Width * lifePercentage);
            g.FillRectangle(Brushes.Green, X, Y - 10, lifeBarWidth, 5);

            Move();
        }

        public Rectangle GetRectangle()
        {
            return new Rectangle(X, Y, Width, Height);
        }

        public void Boom()
        {
            Life--;
            if (Life <= 0)
            {
                IsDead = true;
            }
        }

        public bool HasExitedCanvas()
        {
            // 检查是否超出画布底部
            return Y > CanvasHeight;
        }
    }

}